package com.bdd.jpa.util;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;

/**
 * @Author Bdd
 * @Description
 *      cron
 * @Date 2019/9/24 9:16
 */
public class CronUtil {

    /**
     * "ss mm HH dd MM ? yyyy"
     */
    private static final String CRON_EXPRESSION = "ss mm HH dd MM ? ";

    /***
     *  功能描述：日期转换cron表达式
     * @param date
     * @return
     */
    private static String formatDateByPattern(LocalDateTime date) {
        String formatTimeStr = null;
        if (Objects.nonNull(date)) {
            formatTimeStr = date.format(DateTimeFormatter.ofPattern(CRON_EXPRESSION));
        }
        return formatTimeStr;
    }

    /**
     * "0 15 10 ? * *"
     * convert Date to cron, eg "0 07 10 15 1 ?"
     *
     * @param date : 时间点
     * @return
     */
    public static String getCron(LocalDateTime date) {
        return formatDateByPattern(date);
    }

    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(getCron(dateTime));
    }

}
